package me.xfly.xxplayer;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class MainActivity extends AppCompatActivity {

    String path = "";
    TextView textView;
    final int REQUEST_CODE = 1024;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int permission = ActivityCompat.checkSelfPermission(MainActivity.this,
                        "android.permission.READ_EXTERNAL_STORAGE");
                if (permission != PERMISSION_GRANTED) {
                    return;
                }
                PlayActivity.startActivity(path, MainActivity.this);
            }
        });

        textView = findViewById(R.id.text);

        // 可以播放本地视频、网络视频和直播流
        //eg 1 本地视频
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/小宵虎南-SSIS-621-C.mp4";
        //eg 2 网络视频
        path = "http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4";
        //eg 3 直播流
         path = "https://openhls-tct.douyucdn2.cn/dyliveflv1/1863767rkpl2.flv?uuid=";

        //path = "https://akm-tct.douyucdn.cn/live/290935r6IMHUPSs8.flv?uuid=";

        textView.setText(path);
        //verifyStoragePermissions(this);
        //PlayActivity.startActivity(path, MainActivity.this);
        requestPermissionIfNeeded();

    }

    private String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE"};


    private void requestPermissionIfNeeded() {
        if (!hasPermission()) {
            requestPermission();
            return;
        }
        requestManagerPermission();
    }

    public void requestPermission() {

        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(this,
                    "android.permission.READ_EXTERNAL_STORAGE");
            if (permission != PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_CODE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (hasPermission()) {
                requestManagerPermission();
                Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
            }
        }
    }

    private boolean hasPermission() {
        int permission = ActivityCompat.checkSelfPermission(this,
                "android.permission.READ_EXTERNAL_STORAGE");
        if (permission != PERMISSION_GRANTED) {
            return false;
        }
        return true;
    }

    private void requestManagerPermission() {
        if (Build.VERSION.SDK_INT >= 30) {
            if (!Environment.isExternalStorageManager()) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                startActivity(intent);
            }
        }
    }

}